home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: 26 Feb 1996 08:48:40 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4gsdno$1bg@umbc9.umbc.edu>
- References: <4gqpa1$3h9@alcor.usc.edu>
- NNTP-Posting-Host: umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Abu Wawda <wawda@alcor.usc.edu> wrote:
- |> I'm having trouble understanding what the address of a static array
- |> is.
-
- I'm having trouble understanding why it matters? You almost never use the
- address of an array directly unless doing something tricky with pointers
- or with particular dimensions of a multiple dimensional array.
-
- |> For example, if I declare a variable called myarray as:
- |> char myarray[10];
- |> then what could &myarray possibly mean? myarray is not a pointer, so
- |> &myarray could not possibly be the address of the variable myarray
- |> (like it would be if I did char* myarray and then asked for &myarray).
-
- Yes it could and yes it is...'myarray' is not a pointer, but &myarray is
- a pointer to 'myarray'.
-
- |> Functions such as scanf() allow the following:
- |>
- |> char myarray[10];
- |>
- |> scanf("%s",&myarray);
-
- "Allow" and "produce defined results" are two different things. I believe
- you want simply 'scanf ("%s", myarray);'. Also make sure <stdio.h> is
- included.
-
- |> but I don't understand what scanf() could possibly be taking in the
- |> second parameter. It can't be: char** since myarray is not a
- |> pointer. I CAN understand how the following would work:
- |>
- |> char* myarray;
- |>
- |> myarray = (char*) malloc(10);
- |> scanf("%s",&myarray);
-
- That's incorrect as well for several reasons...
-
- 1) You didn't include <stdlib.h> as far as readers of comp.lang.c can tell
- so your cast is possibly masking an error.
- 2) You don't assure malloc() does not fail.
- 3) Get rid of the &!!!
-
- |> Then scanf() is simply taking a char** (pointer to a character
- |> pointer, or in other words the address of the pointer myarray) in its
- |> second paremeter. However, if I write my own function like this:
- |>
- |> void func(char** p)
- |> {
- |> // do something here
- |> }
- |>
- |> I cannot pass &myarray if I declare it as: char myarray[10]. So how do
- |> this work? Thanks in advance,
-
- Sure you can...Read the FAQ! Particularly sections on pointers and arrays.
-
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-